package com.xyc.servlet.main;

import com.xyc.mapper.SongListMapper;
import com.xyc.mapper.SongMapper;
import com.xyc.mapper.UserMapper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/admin")
public class AdminServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询管理员数量，用户数量，单曲数量，在线用户数，总播放量
        //显示完成后 跳转页面至 admin.jsp
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-dao.xml");
        UserMapper userMapper = ctx.getBean("userMapper", UserMapper.class);
        SongMapper songMapper = ctx.getBean("songMapper", SongMapper.class);
        SongListMapper songListMapper = ctx.getBean("songListMapper", SongListMapper.class);

        int count = userMapper.selectUserCount();
        int count2 = songMapper.selectSongCount();
        int count3 = songListMapper.selectCount();

        req.setAttribute("userCount", count);
        req.setAttribute("songCount", count2);
        req.setAttribute("songListCount", count3);
        //System.out.println("显示用户数量:"+count);
        req.getRequestDispatcher("admin.jsp").forward(req, resp);
    }
}
